package com.songmao.pricecloudapitest.server;

import com.songmao.pricecloudapitest.PricecloudapitestApplication;
import com.songmao.pricecloudapitest.config.PriceCloudApiConfig;
import com.songmao.pricecloudapitest.requestcore.HttpReuqestCore;
import com.songmao.pricecloudapitest.requestcore.ResponePoJo;
import com.songmao.pricecloudapitest.requestcore.UserToken;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Remy
 * @description :
 * @date 2020-05-08 15:15
 */
@Component
@RunWith(SpringRunner.class)
@SpringBootTest(classes = PricecloudapitestApplication.class)
public class PriceCloudHttpCore extends AbstractTransactionalTestNGSpringContextTests {
    @Autowired
    private PriceCloudApiConfig priceCloudApiConfig;

    @Autowired
    private HttpReuqestCore httpReuqestCore;

//    public void priceCloudAdminlogin() {
//        Map<String, String> loginMap = new HashMap<>(6);
//        ResponePoJo rs = httpReuqestCore.get(priceCloudApiConfig.getTestIp());
//        System.out.println(rs);
//        loginMap.put("_xsrf", rs.getCookiesMap().get("_xsrf"));
//        loginMap.put("to_appid", rs.getResponseMap().get("to_appid"));
//        loginMap.put("nonce_str", rs.getResponseMap().get("nonce_str"));
//        loginMap.put("sign", rs.getResponseMap().get("sign"));
//        loginMap.put("email", priceCloudApiConfig.getUsername());
//        loginMap.put("password", priceCloudApiConfig.getPassword());
//        ResponePoJo info = httpReuqestCore.post(rs.getResponseMap().get("Location"), loginMap);
//        UserToken.INFO = "info=" + info.getCookiesMap().get("info");
//    }

    public ResponePoJo get(String path) {
        Map<String, String> map = new HashMap<>();
        if (UserToken.INFO != null) {
            map.put("Cookie", UserToken.INFO);
        }
        return httpReuqestCore.get(processUrl(path), map);
    }

    public ResponePoJo post(String path, String json) {
        Map<String, String> map = new HashMap<>();
        if (UserToken.INFO != null) {
            map.put("Cookie", UserToken.INFO);
        }
        return httpReuqestCore.post(processUrl(path), json, map);
    }

    public ResponePoJo put(String path, String json) {
        Map<String, String> map = new HashMap<>();
        if (UserToken.INFO != null) {
            map.put("Cookie", UserToken.INFO);
        }
        return httpReuqestCore.put(processUrl(path), json, map);
    }

    public ResponePoJo delete(String path) {
        Map<String, String> map = new HashMap<>();
        if (UserToken.INFO != null) {
            map.put("Cookie", UserToken.INFO);
        }
        return httpReuqestCore.delete(processUrl(path), map);

    }

    private String processUrl(String url) {
        if (url != null) {
            try {
                URI uri = new URI(url);
                //若用户填写的url有域名则直接返回
                if (uri.getHost() != null) {
                    return uri.toASCIIString();
                }

                //若用户填写的url只有路径则按照项目内的域名+路径进行拼接直接返回
                if (uri.getPath() != null) {
                    return priceCloudApiConfig.getTestIp() + uri.toASCIIString();
                }

                throw new RuntimeException("error is url");

            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        }

        throw new RuntimeException("error is url");
    }




}
